<?php
/**
 * Created by PhpStorm.
 * User: Lzhy ysjywz@gmail.com
 * Date: 2018/6/4
 * Time: 下午5:25
 */
namespace Core\common;

class Assist
{
    /**
     * 生成url
     * @param $do
     * @param array $query
     * @return string
     */
    public static function gMobileUrl($do,$query = [])
    {
        if(strpos($do,'http') === 0){
            return $do;
        }

        $query = self::_buildQuery($do,$query);
        return str_replace('/./','/','/app/'.murl('entry',$query));
    }

    /**
     * 生成url
     * @param $do
     * @param array $query
     * @return string
     */
    public static function gWebUrl($do,$query = [])
    {
        if(strpos($do,'http') === 0){
            return $do;
        }

        $segment = 'site/entry';
        if(strpos($do,'/') === 0){
            $segment = ltrim($do,'/');
            $do = '';
        }

        $query = self::_buildQuery($do,$query);
        return str_replace('/./','/','/web/'.wurl($segment, $query));
    }

    /**
     * build query
     * @param $do
     * @param array $query
     * @return array
     */
    private static function _buildQuery($do,$query = [])
    {
        if($do){
            $_action = self::globalVal('yWe7.action_name');
            if(strpos($do,'/')){
                $_dArr = explode('/',$do);
                $do = $_dArr[0];
                $query[$_action] = $_dArr[1];
            }
            $query['do'] = $do;
        }
        $query['m']  = Request::params('m');

        return $query;
    }

    /**
     * 设置/获取全局变量
     * @param $key
     * @param null $value
     * @param bool $more
     * @return mixed
     */
    public static function globalVal($key,$value = null,$more = false){
        if($key){
            global $_W;
            $keyArr = explode('.',$key);
            if(empty($value)){
                if($key === '.'){
                    return $_W;
                }
                $_value = $_W;
                foreach ($keyArr as $v){
                    $_value = $_value[$v];
                }
                if(empty($_value) && $more){
                    $_value = self::gpcVal($key);
                }

                return $_value;
            }

            return self::_setGlobalVal($keyArr,$value,$_W);
        }
    }

    /**
     * 设置global值
     * @param $keyArr
     * @param $value
     * @param $pVal
     * @return bool
     */
    private static function _setGlobalVal($keyArr,$value,&$pVal)
    {
        $_keyArr  = $keyArr;
        $firstKey = array_shift($_keyArr);
        $pVal = &$pVal[$firstKey];
        if($_keyArr){
            return self::_setGlobalVal($_keyArr,$value,$pVal);
        }
        $pVal = $value;

        return true;
    }

    /**
     * 查找gpc值
     * @param $key
     * @param string $default
     * @return mixed
     */
    public static function gpcVal($key,$default = '')
    {
        if($key){
            global $_GPC;
            if($key === '.'){
                return $_GPC;
            }
            $keyArr = explode('.',$key);
            $_value = $_GPC;
            foreach ($keyArr as $v){
                $_value = $_value[$v];
            }

            if(empty($_value)){
                $_value = $default;
            }

            return $_value;
        }
    }

    /**
     * 获取uniacid
     * @return null|string
     */
    public static function uniacid()
    {
        $uAcId = self::globalVal('uniacid');
        if(empty($uAcId)){
            $uAcId = self::globalVal('acid');
            if(empty($uAcId)){
                $uAcId = self::globalVal('weid');
                if(empty($uAcId)){
                    $uAcId = self::gpcVal('i');
                }
            }
        }
       
        return $uAcId;
    }
}